package com.tobeface.wechat.message;

import com.tobeface.modules.lang.IOs;
import com.tobeface.modules.lang.Langs;
import com.tobeface.modules.lang.Requires;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

/**
 * @author loudyn
 */
public final class OutputStreamMessageChannel extends AbstractMessageChannel {

    private final OutputStream out;

    /**
     * @param out
     */
    public OutputStreamMessageChannel(OutputStream out) {
        Requires.notNull(out);
        this.out = out;
    }

    @Override
    public void write(String result, Charset charset) {
        IOs.write(out, result.getBytes(charset));
    }

    @Override
    public void flush() {
        try {
            out.flush();
        } catch (IOException e) {
            throw Langs.toUncheck(e);
        }
    }
}
